<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="c_rt" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Fuel Program - Directions</title>
<style>
html,body,#map-canvas {
	height: 100%;
	margin: 0px;
	padding: 0px
}

#panel {
	position: absolute;
	top: 5px;
	left: 50%;
	margin-left: -180px;
	z-index: 5;
	background-color: #fff;
	padding: 5px;
	border: 1px solid #999;
}
</style>
<script
	src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
	var rendererOptions = {
		draggable : true
	};
	var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
	var directionsService = new google.maps.DirectionsService();
	var map;
	/*var strStations = "<c_rt:out value="${gasStations}" escapeXml="false"/>";
	var jsonStations = JSON.stringify(eval("(" + strStation + ")"));*/
	
	function initialize() {
		var centerPoint, geoPoint = null;
		var address = '${param.sourcePlace}';
		geocoder = new google.maps.Geocoder();

		geocoder.geocode({
			'address' : address
		}, function(results, status) {
			if (results != null && results.length >= 1) {
				centerPoint = new google.maps.LatLng(
						results[0].geometry.location.lat(),
						results[0].geometry.location.lng());
				
				var mapOptions = {
						zoom : 7,
						center : centerPoint
					};
				map = new google.maps.Map(document.getElementById('map-canvas'),
							mapOptions);
				directionsDisplay.setMap(map);
				directionsDisplay.setPanel(document.getElementById('directionsPanel'));

				google.maps.event.addListener(directionsDisplay, 'directions_changed',
						function() {
							computeTotalDistance(directionsDisplay.directions);
				});

					
			}
		});
	}

	function computeTotalDistance(result) {
		var total = 0;
		var myroute = result.routes[0];
		for (i = 0; i < myroute.legs.length; i++) {
			total += myroute.legs[i].distance.value;
		}
		total = total / 1000.
		document.getElementById("total").innerHTML = total + " km";
	}

	function calcRoute() {
		var start = "${param.sourcePlace }";
		var end = "${param.destPlace }";
		var request = {
			origin : start,
			destination : end,
			avoidTolls : true,
			travelMode : google.maps.TravelMode.DRIVING
		};
		directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(response);
				addMarkers();
			}
		});
	}
	function addMarkers() {
		var markerLocation;
		geocoder = new google.maps.Geocoder();

		<c_rt:forEach var="station" items="${ gasStations}" varStatus="status">
			address = "${station.fullAddress}";
			geocoder.geocode({
				'address' : address
			}, function(results, status) {
				if (results != null && results.length >= 1) {
					markerLocation = new google.maps.LatLng(
							results[0].geometry.location.lat(),
							results[0].geometry.location.lng());
					var marker = new google.maps.Marker({
						position : markerLocation,
						map : map,
						title : "${station.fullAddress} - ${station.PPG}$"
					});
				}
			});
		</c_rt:forEach>
	}

	google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body onload="calcRoute();">
	<div id="map-canvas" style="float: left; width: 70%; height: 100%"></div>
	<div id="directionsPanel" style="float: right; width: 30%;">
		<p>
			Total Distance: <span id="total"></span>
		</p>
	</div>
</body>
</html>

